package com.wll.store.user.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wll.store.common.result.PageResult;
import com.wll.store.common.result.Result;
import com.wll.store.user.dto.AddrPageInfo;
import com.wll.store.user.entity.Address;
import com.wll.store.user.service.AddressService;
import com.wll.store.user.service.UserService;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sdx2009
 * @since 2022-09-06
 */
@RestController
@RequestMapping("/api/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    //用户地址遍历显示
    @GetMapping("/addrList")
    @RequiresAuthentication
    @RequiresRoles("admin")
//    @Cacheable(value = "addrInfo")
    public PageResult<Address> addrList(AddrPageInfo addrPageInfo){
        Page<Address> addrPage = addressService.getAddrPage(addrPageInfo);
        PageResult<Address> pageResult = new PageResult<>(200, "操作成功", addrPage.getTotal(), addrPage.getRecords(),
                addrPage.getPages(), addrPage.getCurrent());
        return pageResult;
    }

    //添加用户地址
    @PostMapping("/addAddress")
//    @CachePut(value = "addrInfo")
    public Result<?> addAddress(@RequestBody Address address){
        Boolean result = addressService.addAddr(address);
        if (result){
            return new Result<>().success("添加成功");
        }else {
            return new Result<>().error("添加失败");
        }
    }

    //修改用户地址数据
    @PostMapping("/updateAddr")
//    @CachePut(value = "addrInfo")
    public Result<?> updateAddrList(@RequestBody Address address){
        Boolean result = addressService.updateAddrInfoByname(address);
        if (result){
            return new Result<>().success();
        }else {
            return new Result<>().error();
        }
    }

    /*根据用户id删除地址*/
    @GetMapping("/deleteAddr")
//    @CacheEvict(value = "addrInfo",allEntries = true)
    public Result<?> deleteAddr(Integer id){
        Boolean result = addressService.deleteAddrInfoById(id);
        if (result){
            return new Result<>().success("删除成功");
        }else {
            return new Result<>().error("删除失败");
        }
    }

}
